package com.af.community.smart.business

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.af.community.smart.base.*
import com.af.community.smart.model.CommonData
import com.af.community.smart.model.ResultData
import com.af.community.smart.utils.*
import com.af.community.smart.viewmodel.VerifyViewModel
import kotlinx.android.synthetic.main.activity_verify.*
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import kotlinx.android.synthetic.main.layout_title_search_no_hint.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.textColor
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class VerifyActivity : BaseActivity() {

    private val peopleList by lazy {
        listOf(
            ResultData("全部", ""),
            ResultData("常住人口", "1"),
            ResultData("流动人口", "2")
        )
    }
    private val stateList by lazy {
        listOf(
            ResultData("全部", ""),
            ResultData("已审核", "1"),
            ResultData("待审核", "9")
        )
    }
    private var peopleType = ""
    private var stateType = ""
    private var mKey = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_verify)
        setToolbarVisibility(false)
        initTitle()

        stateType = "9"
        stateList[2].isChecked = true
        swipe_refresh.isRefreshing = true
        getData(pageNum)

        observe("verify_check") { updateList() }
    }

    override fun getViewModel() = getViewModel<VerifyViewModel>()

    override fun initTitle(title: String, name: String) {
        search_edit.hint = "请输入姓名或身份证号"
        empty_hint.text = "暂无相关自由申报信息！"
        swipe_refresh.refresh { getData(1) }
        recycle_list.loadLinear(baseContext, swipe_refresh) {
            if (!isLoadingMore && pageNum <= pageTotal) {
                isLoadingMore = true
                getData(pageNum)
            }
        }

        mAdapter = SlimAdapter.create()
            .register<CommonData>(R.layout.item_people_list) { data, injector ->
                injector.visible(R.id.item_people_state)
                    .text(R.id.item_people_name, data.userName)
                    .text(R.id.item_people_id, data.account.idCardReplaceWithStar())
                    .text(R.id.item_people_unit, data.gridName)
                    .text(
                        R.id.item_people_type,
                        when (data.base_type) {
                            "1" -> "常住人口"
                            "2" -> "流动人口"
                            else -> ""
                        }
                    )
                    .with<TextView>(R.id.item_people_state) {
                        it.text = when (data.status) {
                            "1" -> {
                                it.textColor = getColorEx(R.color.colorAccent)
                                "已审核"
                            }
                            "9" -> {
                                it.textColor = getColorEx(R.color.red)
                                "待审核"
                            }
                            else -> ""
                        }
                    }
                    .clicked(R.id.item_people) {
                        if (data.base_type.isNullOrEmpty()) return@clicked
                        startActivity<VerifyDetailActivity>(
                            "id" to data.id,
                            "type" to data.base_type,
                            "status" to data.status
                        )
                    }
            }
            .attachTo(recycle_list)

        verify_type.oneClick {
            getViewModel().showDropFilter(
                baseContext,
                verify_type_hint,
                verify_type_arr,
                verify_head,
                peopleList
            ) {
                peopleType = it.type
                updateList()
            }
        }

        verify_state.oneClick {
            getViewModel().showDropFilter(
                baseContext,
                verify_state_hint,
                verify_state_arr,
                verify_head,
                stateList
            ) {
                stateType = it.type
                updateList()
            }
        }

        search_close.oneClick { search_edit.setText("") }
        search_edit.addTextChangedListener(this@VerifyActivity)
        search_edit.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                hideSoftInput() //隐藏软键盘

                if (search_edit.text.isBlank()) {
                    toast("请输入姓名或身份证号")
                } else {
                    mKey = search_edit.trimString()
                    updateList()
                }
            }
            return@setOnEditorActionListener false
        }

    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().getVerifyList(
            "gridId" to getString("gridId"),
            "search" to mKey,
            "type" to peopleType,
            "status" to stateType,
            "page" to index
        )
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    pageTotal = it.pages
                    mList.apply {
                        if (index == 1) {
                            clear()
                            pageNum = index
                        }
                        addItems(it.list)
                        if (count(it.list) > 0) pageNum++
                    }

                    mAdapter.updateData(mList)
                },
                onFinally = {
                    swipe_refresh.isRefreshing = false
                    isLoadingMore = false
                    empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                }
            )
    }

    private fun updateList() {
        swipe_refresh.isRefreshing = true

        empty_view.gone()
        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        pageNum = 1
        getData(pageNum)
    }

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        search_close.visibility = if (s.isEmpty()) View.GONE else View.VISIBLE
        if (s.isEmpty() && mKey.isNotEmpty()) {
            mKey = ""
            updateList()
        }
    }

}
